package com.guigu.qingcheng.servicebusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.bussiness.TbActivity;
import com.guigu.qingcheng.servicebusiness.mapper.TbActivityMapper;
import com.guigu.qingcheng.servicebusiness.service.TbActivityService;
import com.guigu.qingcheng.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbActivityServiceImpl extends ServiceImpl<TbActivityMapper, TbActivity> implements TbActivityService {

    @Resource
    private TbActivityMapper tbActivityMapper;

    @Override
    public R findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        Page<TbActivity> tbActivityPage = new Page<>(page,size);
        QueryWrapper queryWrapper = createQueryWrapper(searchMap);
        tbActivityMapper.selectPage(tbActivityPage,queryWrapper);
        long total = tbActivityPage.getTotal();
        List<TbActivity> records = tbActivityPage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    @Override
    public TbActivity findById(Integer id) {
        return tbActivityMapper.selectById(id);
    }

    @Override
    public void insert(TbActivity tbActivity) {
        tbActivityMapper.insert(tbActivity);
    }

    @Override
    public void update(TbActivity tbActivity) {
        tbActivityMapper.updateById(tbActivity);
    }

    @Override
    public void delete(Integer id) {
        tbActivityMapper.deleteById(id);
    }


    // 根据查询条件封装模块对象
    private QueryWrapper createQueryWrapper(Map<String, Object> searchMap) {
        QueryWrapper<TbActivity> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id", searchMap.get("id"));
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title")))
                wrapper.like("title", "%" + searchMap.get("title") + "%");
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")))
                wrapper.ge("startTime",searchMap.get("startTime"));
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime")))
                wrapper.le("endTime",searchMap.get("endTime"));
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status")))
                wrapper.eq("status",searchMap.get("status"));
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content")))
                wrapper.eq("content",searchMap.get("content"));
        }
        return  wrapper;
    }
}
